package provider.controller;

import entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import org.springframework.web.bind.annotation.*;
import vo.ResponseResult;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "用户管理",description = "用户相关的API操作")
@RestController
@RequestMapping("/user")
public class UserController {

    private static Map<Integer, User> users=new HashMap<>();

    private static Integer id=1;

    @Operation(summary = "添加用户",description = "添加")
    @Parameters({
            @Parameter(name = "username",description = "用户名"),
            @Parameter(name = "password",description = "密码")
    })
    @ApiResponse(responseCode = "200",description = "返回ResponseResult")
    @PostMapping
    public ResponseResult add(User user){
        System.out.println("Provider.UserController.add()");
        user.setId(id++);
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @Operation(summary = "删除用户",description = "删除")
    @Parameter(name = "id",description = "编号")
    @DeleteMapping("/{id}")
    public ResponseResult remove(@PathVariable("id") Integer id){
        System.out.println("Provider.UserController.remove()");
        users.remove(id);
        return ResponseResult.success();
    }

    @Operation(summary = "修改用户",description = "修改")
    @PutMapping
    public ResponseResult modify(@Parameter(description = "用户信息") User user){
        System.out.println("Provider.UserController.modify()");
        users.put(user.getId(),user);
        return ResponseResult.success();
    }

    @Operation(summary = "根据id查询用户",description = "根据id查询")
    @Parameter(name = "id",description = "编号")
    @GetMapping("/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id){
        System.out.println("Provider.UserController.findById()");
        User user=users.get(id);
        return ResponseResult.success(user);
    }

    @Operation(summary = "查询所有用户",description = "查询所有")
    @GetMapping
    public ResponseResult findAll(){
        System.out.println("Provider.UserController.findAll()");
        Collection<User> Collection=users.values();
        return ResponseResult.success(Collection);
    }

}
